package com.zsk.shop.config.interceptor;

import com.alibaba.fastjson.JSON;
import com.zsk.shop.common.annotion.NoPermission;
import com.zsk.shop.common.base.BaseResponse;
import com.zsk.shop.common.base.BizException;
import com.zsk.shop.common.base.BizExceptionEnum;
import com.zsk.shop.common.utils.TokenUtil;
import com.zsk.shop.common.utils.ToolUtil;
import com.zsk.shop.common.base.model.token.TokenInfoModel;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Component;
import org.springframework.web.method.HandlerMethod;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;

/**
 * @author yuwenbo
 * @description:
 * @date 2019-05-25 11:23
 */
@Component
@Slf4j
public class LoginInterceptor implements HandlerInterceptor {

    @Override
    public boolean preHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o) throws Exception {
        NoPermission noPermission = null;
        try {
            if (!(o instanceof HandlerMethod)) {
                return false;
            }
            HandlerMethod handlerMethod = (HandlerMethod) o;
            noPermission = handlerMethod.getMethodAnnotation(NoPermission.class);
            if (noPermission == null) {
                loginCheck(httpServletRequest, httpServletResponse);
            }
        } catch (BizException e) {
            renderJson(httpServletRequest, httpServletResponse, new BaseResponse(e.getCode(), e.getMessage(), "", "", 0));
            return false;
        }
        return true;
    }

    @Override
    public void postHandle(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, ModelAndView modelAndView) throws Exception {
    }

    @Override
    public void afterCompletion(HttpServletRequest httpServletRequest, HttpServletResponse httpServletResponse, Object o, Exception e) throws Exception {
    }

    private void loginCheck(HttpServletRequest request, HttpServletResponse response) throws BizException {
        System.out.println("自定义过滤器filter2触发,需要拦截token,拦截url:" + request.getRequestURI());
        String auth = "";
//        if (request.getRequestURI().contains("/h5")) {
//            Map<String, String[]> map = request.getParameterMap();
//            auth = JSON.toJSONString(map.get("token"));
//            auth = auth.substring(2, auth.length() - 2);
//        }
        auth = request.getHeader("Authorization");
        if (ToolUtil.isEmpty(auth)) {
            throw new BizException(BizExceptionEnum.TOKEN_ERROR.getCode(), BizExceptionEnum.TOKEN_ERROR.getMessage());
        }
        try {
            TokenInfoModel tokenInfoModel = TokenUtil.verifyToken(auth);
            if (ToolUtil.isEmpty(tokenInfoModel)) {
                throw new BizException(BizExceptionEnum.TOKEN_EXPIRED.getCode(), BizExceptionEnum.TOKEN_EXPIRED.getMessage());
            }
        } catch (Exception e) {
            throw new BizException(BizExceptionEnum.TOKEN_EXPIRED.getCode(), BizExceptionEnum.TOKEN_EXPIRED.getMessage());
        }
    }

    private static void renderJson(HttpServletRequest request, HttpServletResponse response, Object jsonObject) {
        try {
            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            PrintWriter writer = response.getWriter();
            writer.write(JSON.toJSONString(jsonObject));
        } catch (IOException var3) {
            throw new BizException(302, "系统异常302");
        }
    }
}
